package com.zhdl.modules.prefiresys.service.impl;


import com.project.common.service.impl.BaseServiceImpl;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import com.zhdl.modules.prefiresys.entity.DetectorAnalogRec;
import com.zhdl.modules.prefiresys.entity.QDetectorAnalogRec;
import com.zhdl.modules.prefiresys.repository.DetectorAnalogRecRepository;
import com.zhdl.modules.prefiresys.service.DetectorAnalogRecService;
import com.zhdl.modules.prefiresys.web.request.AnalogRecListRequest;
import com.zhdl.modules.prefiresys.web.response.AnalogRecListResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class DetectorAnalogRecServiceImpl extends BaseServiceImpl<String, DetectorAnalogRec, DetectorAnalogRecRepository> implements DetectorAnalogRecService {
    /**
     * 列表查询探测器模拟量明细数据
     *
     * @param analogRecListRequest
     * @return
     */
    @Override
    public List<AnalogRecListResp> findAllRecord(AnalogRecListRequest analogRecListRequest) {
        QDetectorAnalogRec qDetectorAnalogRec = QDetectorAnalogRec.detectorAnalogRec;
        JPAQuery<AnalogRecListResp> query = jpaQueryFactory()
                .select(Projections.constructor(AnalogRecListResp.class,
                        qDetectorAnalogRec.id,
                        qDetectorAnalogRec.detectorId,
                        qDetectorAnalogRec.areaId,
                        qDetectorAnalogRec.createDate,
                        qDetectorAnalogRec.particleCount,
                        qDetectorAnalogRec.particlePercent,
                        qDetectorAnalogRec.smokeCount,
                        qDetectorAnalogRec.smokePercent,
                        qDetectorAnalogRec.serviceVoltage,
                        qDetectorAnalogRec.samplingFlow1,
                        qDetectorAnalogRec.samplingFlow2,
                        qDetectorAnalogRec.samplingFlow3,
                        qDetectorAnalogRec.samplingFlow4,
                        qDetectorAnalogRec.envTemperature,
                        qDetectorAnalogRec.envHumidity,
                        qDetectorAnalogRec.vacuumPressure,
                        qDetectorAnalogRec.detectPressure))
                .from(qDetectorAnalogRec);
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qDetectorAnalogRec.createDate.between(analogRecListRequest.getStartTime(), analogRecListRequest.getEndTime()));
        builder.and(qDetectorAnalogRec.detectorId.eq(analogRecListRequest.getDetectorId()));
        builder.and(qDetectorAnalogRec.areaId.eq(analogRecListRequest.getAreaId()));

        return query.where(builder)
                .orderBy(qDetectorAnalogRec.createDate.asc())
                .fetch();
    }
}
